package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISensorParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Tcp定时任务
 *
 * @author
 */
@Component("tcpTestTask")
public class TcpTestTask {

    private static List<Socket> clientList;
    private static Socket clientSocket;
    @Autowired
    private ISensorParametersService sensorParametersService;
    private static ServerSocket serverSocket;


    private static DataOutputStream dos;

    private static InputStream ips;
    private static OutputStream ops;

    private static int j;

    private static String[] sites = {"01", "02", "03"};

    public static void main(String[] args) {
        String str = "66 03 00 00 00 01 8C 1D";

        BigDecimal num = sendAndReceive("01",str);
        System.out.println(num);
    }

    public static BigDecimal hexStringToBigDecimalQx(String hex){
        String hexString = "";
        hex = hex.substring(9,14);
        hex = hex.replaceAll(" ","");
        System.out.println("返回字节："+hex);
        int intBits = Integer.parseInt(hex, 16);
        BigDecimal bd = BigDecimal.valueOf(intBits).divide(new BigDecimal(100));
        // 设置舍入模式为四舍五入，并保留两位小数
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        System.out.println("转换后："+bd);
        return bd;
    }

    public static BigDecimal hexStringToBigDecimalQxwd(String hex){
        String hexString = "";
        hex = hex.substring(9,14);
        String[] values = hex.split(" ");        // 使用空格作为分隔符分割字符串

//        hex = hexString.replaceAll(" ","");
        System.out.println("返回字节："+hex);
        int h = Integer.parseInt(values[0], 16);
        int l = Integer.parseInt(values[1], 16);
        int b = h * 256 + l;
        if ( b > 32768 ){
            b = b - 65536;
        }
        BigDecimal bd = BigDecimal.valueOf(b).divide(new BigDecimal(100));
        // 设置舍入模式为四舍五入，并保留两位小数
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        System.out.println("转换后："+bd);
        return bd;
    }
    public void TcpServer1() {
        try {
            clientList = new ArrayList<>();
            ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
            serverSocket = new ServerSocket(2317);
            System.out.println("Waiting for clients on port: " + 2317);

            while (clientList.size() < 6) {
                Socket client = serverSocket.accept(); // 接受客户端连接
                System.out.println("Client connected: " + client.getRemoteSocketAddress());
                clientList.add(client);
                // 创建线程处理客户端消息
                Thread thread = new Thread(new ClientHandler(client));
                thread.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class ClientHandler implements Runnable {
        private Socket client;

        public ClientHandler(Socket clientSocket) {
            this.client = clientSocket;
        }

        @Override
        public void run() {
            if (clientList.size() == 6) {
//                broadcast();
            }
        }
    }

//    public void broadcast() {
//        try {
//            for (int k = 0; k < clientList.size(); k++) {
//
//                clientSocket = clientList.get(k);
//                System.out.println("第" + (k + 1) + "个连接------" + clientSocket.getRemoteSocketAddress());
//                dos = new DataOutputStream(clientSocket.getOutputStream());
//
//                ips = clientSocket.getInputStream();     // 有client连上来，打开输入流
//                ops = clientSocket.getOutputStream();   // 打开输出流
//                // 同一个通道，服务端的输出流就是客户端的输入流；服务端的输入流就是客户端的输出流
//                for (String site : sites) {
//                    System.out.println("执行次数----------");
//                    j = 0;
//                    //读取温度值
//                    String wd = "03 15 4A 00 02 E1 D1";
//                    if (site.equals("02")) {
//                        wd = "03 15 4A 00 02 E1 E2";
//                    } else if (site.equals("03")) {
//                        wd = "03 15 4A 00 02 E0 33";
//                    }
//                    String hexString = site + " " + wd; // 需要发送的16进制字符串
//                    BigDecimal temperature = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--温度值：" + temperature);
//                    if (temperature.compareTo(BigDecimal.ZERO) == 0) {
//                        System.out.println("温度是零，跳过");
//                        continue;
//                    }
//                    //读取电导率
//                    String ddl = "03 15 82 00 02 60 2F";
//                    if (site.equals("02")) {
//                        ddl = "03 15 82 00 02 60 1C";
//                    } else if (site.equals("03")) {
//                        ddl = "03 15 82 00 02 61 CD";
//                    }
//                    hexString = site + " " + ddl; // 需要发送的16进制字符串
//                    BigDecimal conductivity = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--电导率值：" + conductivity);
//                    //读取比电导率
//                    String bddl = "03 15 89 00 02 11 ED";
//                    if (site.equals("02")) {
//                        bddl = "03 15 89 00 02 11 DE";
//                    } else if (site.equals("03")) {
//                        bddl = "03 15 89 00 02 10 0F";
//                    }
//                    hexString = site + " " + bddl; // 需要发送的16进制字符串
//                    BigDecimal specificConductivity = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--比电导率值：" + specificConductivity);
//                    //读取盐度
//                    String yd = "03 15 97 00 02 71 EB";
//                    if (site.equals("02")) {
//                        yd = "03 15 97 00 02 71 D8";
//                    } else if (site.equals("03")) {
//                        yd = "03 15 97 00 02 70 09";
//                    }
//                    hexString = site + " " + yd; // 需要发送的16进制字符串
//                    BigDecimal salinity = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--盐度值：" + salinity);
//                    //读取tds
//                    String rjxgt = "03 15 9E 00 02 A1 E9";
//                    if (site.equals("02")) {
//                        rjxgt = "03 15 9E 00 02 A1 DA";
//                    } else if (site.equals("03")) {
//                        rjxgt = "03 15 9E 00 02 A0 0B";
//                    }
//                    hexString = site + " " + rjxgt; // 需要发送的16进制字符串
//                    BigDecimal tds = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--TDS值：" + tds);
//                    //读取PH
//                    String sjd = "03 15 BA 00 02 E1 E2";
//                    if (site.equals("02")) {
//                        sjd = "03 15 BA 00 02 E1 D1";
//                    } else if (site.equals("03")) {
//                        sjd = "03 15 BA 00 02 E0 00";
//                    }
//                    hexString = site + " " + sjd; // 需要发送的16进制字符串
//                    BigDecimal ph = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--PH值：" + ph);
//                    //读取ORP
//                    String orpz = "03 15 C8 00 02 41 F9";
//                    if (site.equals("02")) {
//                        orpz = "03 15 C8 00 02 41 CA";
//                    } else if (site.equals("03")) {
//                        orpz = "03 15 C8 00 02 40 1B";
//                    }
//                    hexString = site + " " + orpz; // 需要发送的16进制字符串
//                    BigDecimal orp = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--ORP值：" + orp);
//                    //读取浊度值
//                    String zhudu = "03 15 F2 00 02 61 F4";
//                    if (site.equals("02")) {
//                        zhudu = "03 15 F2 00 02 61 C7";
//                    } else if (site.equals("03")) {
//                        zhudu = "03 15 F2 00 02 60 16";
//                    }
//                    hexString = site + " " + zhudu; // 需要发送的16进制字符串
//                    BigDecimal turbidity = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--浊度值：" + turbidity);
//                    //读取叶绿素值
//                    String yls = "03 16 A8 00 02 41 A3";
//                    if (site.equals("02")) {
//                        yls = "03 16 A8 00 02 41 90";
//                    } else if (site.equals("03")) {
//                        yls = "03 16 A8 00 02 40 41";
//                    }
//                    hexString = site + " " + yls; // 需要发送的16进制字符串
//                    BigDecimal chl = sendAndReceive(clientSocket, site, hexString);
//                    System.out.println(site + "--叶绿素值：" + chl);
//                    //读取溶解氧
////                String rjy = "03 00 FF 00 02 F4 3B";
////                hexString = site + " " + rjy; // 需要发送的16进制字符串
////                BigDecimal dissolvedOxygen = sendAndReceive(site,hexString);
////                System.out.println("溶解氧浓度值：" + dissolvedOxygen);
////                //读取深度
////                String sd = "03 04 6F 00 02 F5 26";
////                hexString = site + " " + sd; // 需要发送的16进制字符串
////                BigDecimal depth = sendAndReceive(site,hexString);
////                System.out.println("深度值：" + depth);
//                    SensorParameters sensorParameters = new SensorParameters();
//                    sensorParameters.setTemperature(temperature);
//                    sensorParameters.setConductivity(conductivity);
//                    sensorParameters.setSpecificConductivity(specificConductivity);
//                    sensorParameters.setSalinity(salinity);
//                    sensorParameters.setTds(tds);
//                    sensorParameters.setPh(ph);
//                    sensorParameters.setOrp(orp);
//                    sensorParameters.setTurbidity(turbidity);
//                    sensorParameters.setChl(chl);
////                sensorParameters.setDissolvedOxygen(dissolvedOxygen);
////                sensorParameters.setDepth(depth);
//                    sensorParameters.setSite(site);
//                    sensorParameters.setCreateTime(new Date());
//                    sensorParameters.setStatus("1");
//                    sensorParametersService.updateSensorParametersBySite(site);
//                    sensorParametersService.insertSensorParameters(sensorParameters);
//                }
//                dos.close();
//                ips.close();
//                ops.close();
////                clientSocket.close();
//
//            }
//            serverSocket.close();
//            System.out.println("ServerSocket已关闭。");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static BigDecimal sendAndReceive( String site, String hexString) {
        BigDecimal num = new BigDecimal(0);
        try {
            System.out.println("发送的数据：" + hexString);
            byte[] bytes = hexStrToBinaryStr(hexString);
            dos.write(bytes);
            dos.flush();
            //输出两次
            for (int i = j; i < 1; i++) {
                // 等待一段时间，然后再次发送
                Thread.sleep(100);
                j++;
                System.out.println("发送的数据：" + hexString);
                bytes = hexStrToBinaryStr(hexString);
                // 假设我们正在读取寄存器，从地址0开始，读取1个寄存器
                dos.write(bytes);
                dos.flush();

            }
            // 接收数据
            //获取输入流，读数据，并把数据显示到控制台
            InputStream is = clientSocket.getInputStream();
//            byte[] bys = new byte[1024];
//            int len = is.read(bys);
//            bys = Arrays.copyOfRange(bys,0,len);
//            String data = BinaryToHexString(bys);
//            System.out.println("返回数据是：" + data);

            int s = 0;
            //接收服务器的响应
//                if(available==0){
//                    continue;
//                }
            byte[] buf = new byte[1024];
            //接收收到的数据
            String strReturn = "";
            int i = 0;
            while (i < 16) {
                Thread.sleep(500);
                i++;
                int available = is.available();
                if(available>0){
                    if (is.read(buf) != -1) {
                        System.out.println(" available : " + available);
                        //将字节数组转换成十六进制的字符串
                        strReturn = BinaryToHexString(buf);
                        System.out.println("strReturn : " + strReturn);
                        if (!strReturn.isEmpty()) {
                            String str = strReturn.replaceAll(" ", "");
                            String shuju = site + "0304";
                            if (str.contains(shuju)) {
                                str = str.split(shuju)[1].substring(0, 8);
                                System.out.println("返回数据是：" + str);
                                num = hexStringToBigDecimal(str);
                                break;
                            } else {
                                System.out.println("str--- " + str + " -shuju- " + shuju);
//                            flag = false;
                            }
                        } else {
                            System.out.println(" if (!strReturn.isEmpty()) {");
                        }
                    }
                }


            }
//            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//            byte[] dataBuffer = new byte[1024];
//            int bytesRead;
//            while ((bytesRead = ips.read(dataBuffer)) != -1) {
//                buffer.write(dataBuffer, 0, bytesRead);
//            }
//            byte[] data = buffer.toByteArray();
//            System.out.println("接收到的数据：");
//            if (data.length > 0) {
//                String res = BinaryToHexString(data);
//                System.out.print(res);
//                BigDecimal bd = hexStringToBigDecimal(res);
//                return bd;
//            }
//            return new BigDecimal(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    public static String BinaryToHexString(byte[] bytes) {
        String hexStr = "0123456789ABCDEF";
        String result = "";
        String hex = "";
        for (byte b : bytes) {
            hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
            hex += String.valueOf(hexStr.charAt(b & 0x0F));
            result += hex + " ";
        }
        return result;
    }

    public static BigDecimal hexStringToBigDecimal(String hex) {
        String hexString = "";
        hex = hex.substring(9,20);
        String[] values = hex.split(" ");        // 使用空格作为分隔符分割字符串
        for (int i = values.length - 1; i >= 0; i--) { // 倒序遍历数组
            if (i != 0) {
                hexString += values[i] + " "; // 打印值后跟一个空格
            } else {
                hexString += values[i];    // 打印最后一个值后换行
            }
        }
        hex = hexString.replaceAll(" ","");
        System.out.println("返回字节："+hex);
        int intBits = Integer.parseInt(hex, 16);
        float floatValue = Float.intBitsToFloat(intBits);
        BigDecimal bd = BigDecimal.valueOf(floatValue);
        // 设置舍入模式为四舍五入，并保留两位小数
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        System.out.println("转换后："+bd);
        return bd;
    }

    public static byte[] hexStrToBinaryStr(String hexString) {
        if (StringUtils.isEmpty(hexString)) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        int index = 0;
        byte[] bytes = new byte[len / 2];
        while (index < len) {
            String sub = hexString.substring(index, index + 2);
            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
            index += 2;
        }
        return bytes;
    }
}
